package com.guessdroid.UI;

import com.guessdroid.R;
import com.guessdroid.labeling.GContextList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

public class NewContextActivity extends Activity{
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newcontext);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        showVirtualKeyboard();
        Button save = (Button)findViewById(R.id.saveButton);
        save.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					addContext();
					moveToHome();
			    }
		});
        Button cancel = (Button)findViewById(R.id.cancelButton);
        cancel.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					moveToHome();
			    }
		});
    }
    
    private void showVirtualKeyboard(){
    	EditText editText = (EditText) findViewById(R.id.contextNameEdit);
    	InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    	mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
    }
    
    //TODO validation, feedback
    private void addContext(){
    	EditText editText = (EditText) findViewById(R.id.contextNameEdit);
    	String name = editText.getText().toString();
    	if(isValid(name)){
	    	GContextList list = GContextList.read();
	    	list.addContext(name);
	    	list.save();
    	}
    }
    
    private boolean isValid(String name){
    	//null, duplicate
    	return true;
    }
    
    private void moveToHome(){
    	Intent intent = new Intent(NewContextActivity.this, HomeActivity.class);
        startActivity(intent);
    }
	

}
